import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.*;

import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;

public class Test {
    /**
     * 生成的PDF文件的路径。
     */
    public static final String RESULT = "F:\\test.pdf";

    /**
     * 创建一个PDF文件：hello.pdf
     *
     * @param args no arguments needed
     */
    public static void main(String[] args) throws DocumentException, IOException {
//        new Test().createPdf(RESULT);


        HashMap<String, String> map = new HashMap<>();
        map.put("time","123");
        map.put("site","456");
        Util.interviewReportPDF(RESULT,"F:\\new.pdf",map);




//        Test test = new Test();
//        test.createBarcodePdf();


//        Test test = new Test();
//        test.createPdf417();


    }

    /**
     * 创建PDF文档.
     *
     * @param filename 新PDF文档的路径
     * @throws DocumentException
     * @throws IOException
     */
    public void createPdf(String filename) throws DocumentException, IOException {
        // 第一步 创建文档实例
//        Document document = new Document();
//        // 第二步 获取PdfWriter实例
//        PdfWriter.getInstance(document, new FileOutputStream(filename));
//        // 第三步 打开文档
//        document.open();
//        // 第四步 添加段落内容
//        document.add(new Paragraph("Hello World!"));
//        // 第五部 操作完成后必须执行文档关闭操作。
//        document.close();


        PdfReader pdfReader = new PdfReader(filename);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        PdfStamper ps = new PdfStamper(pdfReader, bos);
        AcroFields s = ps.getAcroFields();





    }

    public void createBarcodePdf(){
        try {
            Document document = new Document();
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("F:\\scanner.pdf"));

            document.open();
            PdfContentByte cb = writer.getDirectContent();

            BarcodeQRCode barcodeQRCode = new BarcodeQRCode("barcode test", 1000, 1000, null);
            com.itextpdf.text.Image codeQrImage = barcodeQRCode.getImage();
            codeQrImage.scaleAbsolute(100, 100);

            document.add(codeQrImage);

            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void createPdf417() throws IOException {
        String codeString = "this is a test messagethis is a test messagethis is a test message";
        Barcode128 pdf = new Barcode128();
//        pdf.setText(codeString.getBytes("GBK"));
        java.awt.Image pdfImg = pdf.createAwtImage(Color.black, Color.white);
        BufferedImage img = new BufferedImage(pdfImg.getWidth(null), pdfImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
        Graphics g = img.getGraphics();

        g.drawImage(pdfImg, 0, 0, Color.white, null);
//        OutputStream os = new BufferedOutputStream(new FileOutputStream("C:/Users/admin/Desktop/pdf417.bmp"));
//        ImageIO.write(img, "PNG", os);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(img, "PNG", baos);
        byte[] data = baos.toByteArray();
        InputStream is = new ByteArrayInputStream(baos.toByteArray());
        System.out.println("图像输出流：");
        System.out.println(baos.toByteArray());

        byte2image(data, "F:/pdfss.bmp" );
    }

    //byte数组到图片
    public void byte2image(byte[] data, String path) {
        if (data.length < 3 || path.equals("")) {return;}
        try {
            FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
            imageOutput.write(data, 0, data.length);
            imageOutput.close();
            System.out.println("Make Picture success,Please find image in " + path);
        } catch (Exception ex) {
            System.out.println("Exception: " + ex);
            ex.printStackTrace();
        }
    }

}
